#include "thread-executor.h"


ThreadExecutor::ThreadExecutor()
    : thread_([this] { loop(); }) {

}

ThreadExecutor::~ThreadExecutor() {
    stopped_ = true;
    queue_.wakeupConsumer();
    thread_.join();
}

void ThreadExecutor::post(Task &&task) {
    queue_.push(std::move(task));
}

void ThreadExecutor::loop() {
    while (!stopped_) {
        std::optional<std::function<void()>> task = queue_.pop();
        if (task) {
            task.value()();
        }
    }
}
